package com.rarebot.script.randoms;

import com.rarebot.script.Random;
import com.rarebot.script.ScriptManifest;
import com.rarebot.script.wrappers.RSComponent;
import com.rarebot.script.wrappers.RSInterface;
import com.rarebot.script.wrappers.RSNPC;
import com.rarebot.script.wrappers.RSObject;

/**
 * Jacmob was here to verify that Qauters' spelling mistake will be maintained in his memory.
 *
 * @author Qauters
 */
@ScriptManifest(authors = {"Qauters", "Drizzt1112", "TwistedMind", "Scaper", "Psion"}, name = "SandwichLady", version = 2.5)
public class SandwhichLady extends Random {

	private final static int ID_InterfaceSandwhichWindow = 297;
	private final static int ID_InterfaceSandwhichWindowText = 48;
	private final static int[] ID_Items = {10728, 10732, 10727, 10730, 10726, 45666, 10731};
	private final static int ID_SandwhichLady = 8630;
	private final static String[] Item_Names = {"chocolate", "triangle", "roll", "pie", "baguette", "doughnut", "square"};
	private final static boolean DEBUG = false; // Set to true for more info!

	@Override
	public boolean activateCondition() {
		return npcs.getNearest(ID_SandwhichLady) != null;
	}

	@Override
	public int loop() {
		final RSNPC lady = npcs.getNearest(ID_SandwhichLady);
		if (!activateCondition()) {
			return -1;
		}
		if (getMyPlayer().getAnimation() != -1) {
			return random(500, 1000);
		}
		//Leaves random
		if (interfaces.get(1184).getComponent(13).containsText("That's right") || interfaces.get(1184).getComponent(13).containsText("The exit portal")) {
			final RSObject portal = objects.getNearest(12731, 11373);
			if (portal != null) {
				if (!calc.tileOnScreen(portal.getLocation())) {
					walking.walkTileOnScreen(portal.getLocation());
				} else {
					if (portal.interact("Enter")) {
						return random(2000, 3000);
					}
					return 200;
				}
			}
		}
		// Check if we need to press continue, on the talk interface
		if (interfaces.canContinue()) {
			interfaces.clickContinue();
			return random(900, 1200);
		}
		// Check if the sandwhich window is open
		if (interfaces.get(ID_InterfaceSandwhichWindow).isValid()) {
			final RSInterface window = interfaces.get(ID_InterfaceSandwhichWindow);
			int offset = -1;
			final String txt = window.getComponent(ID_InterfaceSandwhichWindowText).getText();
			for (int off = 0; off < Item_Names.length; off++) {
				if (txt.contains(Item_Names[off])) {
					offset = off;
					if (DEBUG) {
						log.info("Found: " + Item_Names[off] + " - ID: " + ID_Items[off]);
					}
				}
			}
			for (int i = 7; i < 48; i++) {
				final RSComponent inf = window.getComponent(i);
				if (DEBUG) {
					log.info("child[" + i + "] ID: " + inf.getModelID() + " == " + ID_Items[offset]);
				}
				if (inf.getModelID() == ID_Items[offset]) {
					inf.doClick();
					sleep(900, 1200); // Yea, use a sleep here! (Waits are allowed in randoms.)
					if (!interfaces.get(ID_InterfaceSandwhichWindow).isValid()) {
						log.info("Solved the Sandwich Lady, by eating a " + Item_Names[offset]);
						sleep(6000);
						return random(900, 1500);
					}
				}

			}
			return random(900, 1200);
		}
		// Talk to the lady
		if (lady != null && lady.getAnimation() == -1) {
			if (!calc.tileOnScreen(lady.getLocation())) {
				camera.turnTo(lady);
				camera.setPitch(false);
			} else {
				if (lady.interact("talk")) {
					return random(1000, 1500);
				}
			}
		}
		return random(900, 1200);
	}
}